package app;

import service.CarService;
import service.FileService;
import service.HouseService;

import javax.swing.*;
import java.awt.*;

public class ViewInit {

    private JFrame jFrame;

    public ViewInit(){
        jFrame=new JFrame("诉讼系统线下数据补录");
        jFrame.setSize(500,200);
        jFrame.setLocation(300,300);
        jFrame.setLayout(new FlowLayout());
        addControl();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        jFrame.setResizable(false);
    }

    private void addControl(){
        JLabel label=new JLabel("请输入导入的excel路径：");
        JTextField excelPathText=new JTextField();
        excelPathText.setPreferredSize(new Dimension(220,30));

        JLabel label1=new JLabel("请输入生成SQL的路径：");
        JTextField sqlPathText=new JTextField();
        sqlPathText.setPreferredSize(new Dimension(220,30));

        jFrame.add(label);
        jFrame.add(excelPathText);
        jFrame.add(label1);
        jFrame.add(sqlPathText);

        JPanel jPanel=new JPanel();
        jPanel.setPreferredSize(new Dimension(220,30));
        JRadioButton houseRadio=new JRadioButton("房贷单补录");
        houseRadio.setSelected(true);
        JRadioButton carRadio=new JRadioButton("车贷单补录");
        ButtonGroup buttonGroup=new ButtonGroup();
        buttonGroup.add(houseRadio);
        buttonGroup.add(carRadio);
        jPanel.add(houseRadio);
        jPanel.add(carRadio);
        jFrame.add(jPanel);

        JButton button=new JButton("开始执行");
        button.setPreferredSize(new Dimension(250,30));
        button.addActionListener(e->{
            String excelPath=excelPathText.getText();
            String sqlPath=sqlPathText.getText();
            if("".equals(excelPath)){
                JOptionPane.showMessageDialog(jFrame,"excel路径不能为空！","提示",JOptionPane.WARNING_MESSAGE);
                return;
            }
            if("".equals(sqlPath)){
                JOptionPane.showMessageDialog(jFrame,"生成SQL的路径不能为空！","提示",JOptionPane.WARNING_MESSAGE);
                return;
            }
            String sqlContext="";
            if(houseRadio.isSelected()){
                 sqlContext = new HouseService().importExcel(excelPath);
            }
            if(carRadio.isSelected()){
                sqlContext=new CarService().importExcel(excelPath);
            }
            if(FileService.saveFile(sqlPath,sqlContext)){
                JOptionPane.showMessageDialog(jFrame,"执行成功！","提示",JOptionPane.OK_CANCEL_OPTION);
            }else {
                JOptionPane.showMessageDialog(jFrame,"执行失败！","提示",JOptionPane.ERROR_MESSAGE);
            }
        });
        jFrame.add(button);
    }
}
